import AjaxPlugin from '../AjaxPlugin/AjaxPlugin'

export async function login (userName, password) {
  let res
  try {
    res = await AjaxPlugin.$http
      .post('http://dev2.shareg.cn/api/login', {
        telphone: userName,
        code: password
      })
  } catch (e) {
    throw Object({message: '网络错误'})
  }

  if (res && res.data) {
    let content = res.data
    if (content.status_code === 200) {
      store.commit('UPDATE_AUTH_TOKEN', content.result.access_token)
      // TODO: 给缓存设定有效期
      return {success: true, userName}
    } else {
      return {success: false, message: content.message}
    }
  } else {
    return {success: false, message: '登录失败'}
  }
}

export async function sendSMS(tel) {
  let res
  try {
    res = await AjaxPlugin.$http
      .post('http://dev2.shareg.cn/api/send_code', {
        telphone: tel
      })
  } catch (e) {
    throw Object({message: '网络错误'})
  }

  return res && res.data && res.data.status_code === 200
}
